$(function(){

	$("p").attr("contenteditable","false");
	var $li=$(".page-list>li");
	$li.click(function(){
		var $this=$(this);
		var index=$li.index(this);
		$("[name=que]").val(index+1+'');
		$("#nextque").submit();
	})
	//为选项中加上isuser以及isright的类相应的文字添加不同的样式
	var $ansop=$(".ans-op");
	$ansop.each(function(index,item){
		var $this=$(this);
		if ($this.hasClass("isuser")) {
			$this.find('i').removeClass("fa-checked").addClass("fa-times");
			$this.siblings('.que-text').addClass("user-ans-style");
		}
		if ($this.hasClass("isright")) {
			$this.find('i').removeClass("fa-times").addClass("fa-checked");
			$this.siblings('.que-text').addClass("right-ans-style")
		}
	});
    $("#next").click(function () {
        $("#nextque").submit();
    });

    var showtext=[];
    var sourse=[];
	var $model=$("#voicemodel").clone(true).removeClass("hidden").addClass("users-voice").removeAttr("id");
	que_info.num=2;
    $.ajax({
		url:"?r=ajax/comment",
		data:que_info,
		dataType:'json',
		method:'get',
		success:function(data){
			var length=data.length;
            for(var i=0; i<length; i++){
                var $clone=$model.clone(true);
                sourse.push(data[i].comment_content)
                showtext.push(changeText(changeData(data[i].comment_content)));
                setHot(data[i],$clone);
                $clone.insertBefore(".add-more");
                $clone.hide();
                if (i<2) {
                    $clone.slideDown(500);
                }
			}

            //展开全文
            
            var $viewall=$(".view-all");
            var $comment=$(".user-comments");
            var flag=true;
            $comment.addClass("notshow");
            $viewall.click(function(){
                var index=$viewall.index(this);
                var $thiscomment=$(this).siblings(".user-comments");
                if ($thiscomment.hasClass("notshow")) {
                    $thiscomment.html(sourse[index]).removeClass("notshow");
                    $(this).text("点击收起全文");
                }else{
                    $thiscomment.html(showtext[index]).addClass("notshow");
                    $(this).text("点击展开全文");
                }
            });
            var $addmore=$(".add-more");
            $addmore.click(function(){
                var n=0;
                $(".users-voice").each(function(index,item){
                    var $this=$(this);
                    if (!$this.is(":visible")&&n<5) {
                        $this.slideDown(500);
                        n++;
                    }
                })
            });

                    //点赞功能,只能点赞一次,点赞后添加一个hasClick的类

            $(".good-times>span").click(function(){
                var $this=$(this);

                //获取当前所在的下标index
                var $thisvoice=$this.closest(".good-times");
                var index=$(".good-times").index($thisvoice);
                var tarid=data[index].user_id;

                var $thisgood=$this.find(".goods");
                if (!$this.hasClass("isgood")) {
                    $thisgood.text(parseInt($thisgood.text())+1+'');
                    $this.addClass("isgood");
                    $.ajax({
                        url:'?r=ajax/like',
                        method:'post',
                        dataType:'json',
                        data:{
                            'tar':tarid,
                            'sou':current_userid,
                            'class2':que_info.class2,
                            'class1':que_info.class1,
                            'question':que_info.question,
                            'status':'like'
                        }
                    })
                }else{
                    $thisgood.text(parseInt($thisgood.text())-1+'');
                    $this.removeClass("isgood");
                    $.ajax({
                        url:'?r=ajax/like',
                        method:'post',
                        dataType:'json',
                        data:{
                            'tar':tarid,
                            'sou':current_userid,
                            'class2':que_info.class2,
                            'class1':que_info.class1,
                            'question':que_info.question,
                            'status':'unlike'
                        }
                    })
                }
            });

            $(".users-voice").mouseenter(function(){
                var $this=$(this);
                //获取当前所在的下标index
                var index=$(".users-voice").index(this);
                var tarid=data[index].user_id;
                $.ajax({
                    url:'?r=ajax/view',
                        method:'post',
                        dataType:'json',
                        data:{
                            'tar':tarid,
                            'sou':current_userid,
                            'class2':que_info.class2,
                            'class1':que_info.class1,
                            'question':que_info.question
                        }
                })
            })
		}
	});

	//分享
    var $share=$(".share");
    $share.click(function(){
	    var comments=$("#user-comments").html();
	    var que_info_comment=que_info;
	    que_info_comment.comment=comments;
        $succeed.show();
    	$.ajax({
    		method:'post',
    		url:'?r=ajax/comment',
    		dataType:'json',
    		data:que_info_comment,
    		success:function(data){
                if (data.state) {
                    $(".users-voice").remove();
                    var showtext=[];
                    var sourse=[];
                    que_info.num=2;
                    $.ajax({
                        url:"?r=ajax/comment",
                        data:que_info,
                        dataType:'json',
                        method:'get',
                        success:function(data){
                            var length=data.length;
                            for(var i=0; i<length; i++){
                                var $clone=$model.clone(true);
                                sourse.push(data[i].comment_content);
                                showtext.push(changeText(changeData(data[i].comment_content)));
                                setHot(data[i],$clone);
                                $clone.insertBefore(".add-more");
                                $clone.hide();
                                if (i<2) {
                                    $clone.slideDown(500);
                                }
                            }
                            //展开全文
            
                            var $viewall=$(".view-all");
                            var $comment=$(".user-comments");
                            var flag=true;
                            $comment.addClass("notshow");
                            $viewall.click(function(){
                                var index=$viewall.index(this);
                                var $thiscomment=$(this).siblings(".user-comments");
                                if ($thiscomment.hasClass("notshow")) {
                                    $thiscomment.html(sourse[index]).removeClass("notshow");
                                    $(this).text("点击收起全文");
                                }else{
                                    $thiscomment.html(showtext[index]).addClass("notshow");
                                    $(this).text("点击展开全文");
                                }
                            });

                            var $addmore=$(".add-more");
                            $addmore.click(function(){
                                var n=0;
                                $(".users-voice").each(function(index,item){
                                    var $this=$(this);
                                    if (!$this.is(":visible")&&n<5) {
                                        $this.slideDown(500);
                                        n++;
                                    }
                                })
                            });
                            //点赞功能,只能点赞一次,点赞后添加一个hasClick的类
                            $(".good-times>span").click(function(){
                                var $this=$(this);

                                //获取当前所在的下标index
                                var $thisvoice=$this.closest(".good-times");
                                var index=$(".good-times").index($thisvoice);
                                var tarid=data[index].user_id;

                                var $thisgood=$this.find(".goods");
                                if (!$this.hasClass("isgood")) {
                                    $thisgood.text(parseInt($thisgood.text())+1+'');
                                    $this.addClass("isgood");
                                    $.ajax({
                                        url:'?r=ajax/like',
                                        method:'post',
                                        dataType:'json',
                                        data:{
                                            'tar':tarid,
                                            'sou':current_userid,
                                            'class2':que_info.class2,
                                            'class1':que_info.class1,
                                            'question':que_info.question,
                                            'status':'like'
                                        }
                                    })
                                }else{
                                    $thisgood.text(parseInt($thisgood.text())-1+'');
                                    $this.removeClass("isgood");
                                    $.ajax({
                                        url:'?r=ajax/like',
                                        method:'post',
                                        dataType:'json',
                                        data:{
                                            'tar':tarid,
                                            'sou':current_userid,
                                            'class2':que_info.class2,
                                            'class1':que_info.class1,
                                            'question':que_info.question,
                                            'status':'unlike'
                                        }
                                    })
                                }
                            });

                            $(".users-voice").mouseenter(function(){
                                var $this=$(this);

                                //获取当前所在的下标index
                                var index=$(".users-voice").index(this);
                                var tarid=data[index].user_id;
                                $.ajax({
                                    url:'?r=ajax/view',
                                        method:'post',
                                        dataType:'json',
                                        data:{
                                            'tar':tarid,
                                            'sou':current_userid,
                                            'class2':que_info.class2,
                                            'class1':que_info.class1,
                                            'question':que_info.question
                                        }
                                })

                            })
                        }
                    })
                }
            }
    	})
    })

    var $succeed=$("#comment-succeed");
    $succeed.hide();
    $(document).ajaxStop(function(){
        $succeed.hide();
    })


});
function changeText(val){
	if (val.length>120) {
        return val.slice(0,120)+'...';
    }else{
        return val;
    }
}
